/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Department;

import java.sql.*;
import java.util.Vector;

/**
 *
 * @author mishra
 */
public class Query {

    static Connection con = null;

    public static int add(DepartmentBean db) throws SQLException {
        int i = 0, k;
        try {
            con = DBConnection.Connection.getConnection();
            PreparedStatement pre = con.prepareStatement("insert into department values(departmentid.nextval,?,sysdate)");
            pre.setString(1, db.getDepartmentName());
            k = pre.executeUpdate();
            if (k != 0) {
                i = getMaxId();
            }
        } catch (Exception e) {
            System.out.println(e);
        }
        return i;
    }

    public static int getMaxId() throws SQLException {
        int i = 0;
        try {
            PreparedStatement pre = con.prepareStatement("select max(department_id) from department");
            ResultSet rs = pre.executeQuery();
            while (rs.next()) {
                i = rs.getInt(1);
            }

        } catch (Exception e) {
            System.out.println(e);
        }
        return i;
    }

    public static DepartmentBean viewSpecific(String cname, int val) throws SQLException {
        DepartmentBean db = new DepartmentBean();
        try {
            con = DBConnection.Connection.getConnection();
            PreparedStatement pre = con.prepareStatement("select * from department where " + cname + "=?");
            pre.setInt(1, val);
            ResultSet rs = pre.executeQuery();
            while (rs.next()) {
                db.setDepartmentId(rs.getInt(1));
                db.setDepartmentName(rs.getString(2));
                db.setEntryDate(rs.getDate(3));
            }
        } catch (Exception e) {
            System.out.println(e);
        } finally {
            con.close();
        }
        return db;
    }

    public static Vector viewAll() throws SQLException {
        Vector<Object> list = new Vector<Object>();

        try {
            con = DBConnection.Connection.getConnection();
            PreparedStatement pre = con.prepareStatement("select * from department order by department_id");
            ResultSet rs = pre.executeQuery();
            while (rs.next()) {
                DepartmentBean db = new DepartmentBean();
                db.setDepartmentId(rs.getInt(1));
                db.setDepartmentName(rs.getString(2));
                db.setEntryDate(rs.getDate(3));
                list.add(db);
            }
        } catch (Exception e) {
            System.out.println(e);
        } finally {
            con.close();
        }
        return list;
    }

    public static int edit(DepartmentBean db) throws SQLException {
        int i = 0;
        try {
            con = DBConnection.Connection.getConnection();
            PreparedStatement pre = con.prepareStatement("update department set department_name=?, entrydate=sysdate where department_id=?");
            pre.setString(1, db.getDepartmentName());
            pre.setInt(2, db.getDepartmentId());
            i = pre.executeUpdate();

        } catch (Exception e) {
            System.out.println(e);
        } finally {
            con.close();
        }
        return i;
    }

    public static int delete(int id) throws SQLException {
        int i = 0;
        try {
            con = DBConnection.Connection.getConnection();
            PreparedStatement pre = con.prepareStatement("delete from department where department_id=?");
            pre.setInt(1, id);
            i = pre.executeUpdate();
        } catch (Exception e) {
            return i;
        } finally {
            con.close();
        }
        return i;
    }
}
